package com.itboye.sunsun.product.center;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.sunsun.constants.NetPublicConstant;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsunhome.R;

public class ExampleActivity extends BaseActivity
{
    private VideoEnabledWebView webView;

    private VideoEnabledWebChromeClient webChromeClient;
    
    String url;
    ImageView close_icon;
    TextView add_shap_title_tv;
    String title;

    @Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
        setContentView(R.layout.activity_example);
        setStatusBarColor(R.color.home_blue);
        // Save the web view
        webView = (VideoEnabledWebView) findViewById(R.id.webView);
    	url = getIntent().getStringExtra("url");
    	title=getIntent().getStringExtra("title");
    	add_shap_title_tv.setText(title);
    	close_icon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
        // Initialize the VideoEnabledWebChromeClient and set event handlers
        View nonVideoLayout = findViewById(R.id.nonVideoLayout); // Your own
                                                                 // view, read
                                                                 // class
                                                                 // comments
        ViewGroup videoLayout = (ViewGroup) findViewById(R.id.videoLayout); // Your
                                                                            // own
                                                                            // view,
                                                                            // read
                                                                            // class
                                                                            // comments
        // noinspection all
        View loadingView = getLayoutInflater()
                .inflate(R.layout.view_loading_video, null); // Your own view,
                                                             // read class
                                                             // comments
        webChromeClient = new VideoEnabledWebChromeClient(nonVideoLayout,
                videoLayout, loadingView, webView) // See all available
                                                   // constructors...
        {
            // Subscribe to standard events, such as onProgressChanged()...
            @Override
            public void onProgressChanged(WebView view, int progress)
            {
                // Your code...
            }
        };
        webChromeClient.setOnToggledFullscreen(
                new VideoEnabledWebChromeClient.ToggledFullscreenCallback()
                {
                    @Override
                    public void toggledFullscreen(boolean fullscreen)
                    {
                        // Your code to handle the full-screen change, for
                        // example showing and hiding the title bar. Example:
                        if (fullscreen)
                        {
                            WindowManager.LayoutParams attrs = getWindow()
                                    .getAttributes();
                            attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
                            attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                            getWindow().setAttributes(attrs);
                            if (android.os.Build.VERSION.SDK_INT >= 14)
                            {
                                // noinspection all
                                getWindow().getDecorView()
                                        .setSystemUiVisibility(
                                                View.SYSTEM_UI_FLAG_LOW_PROFILE);
                            }
                        }
                        else
                        {
                            WindowManager.LayoutParams attrs = getWindow()
                                    .getAttributes();
                            attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
                            attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                            getWindow().setAttributes(attrs);
                            if (android.os.Build.VERSION.SDK_INT >= 14)
                            {
                                // noinspection all
                                getWindow().getDecorView()
                                        .setSystemUiVisibility(
                                                View.SYSTEM_UI_FLAG_VISIBLE);
                            }
                        }

                    }
                });
        webView.setWebChromeClient(webChromeClient);
        // Call private class InsideWebViewClient
        webView.setWebViewClient(new InsideWebViewClient());

        // Navigate anywhere you want, but consider that this classes have only
        // been tested on YouTube's mobile site
        webView.loadUrl(NetPublicConstant.WEB_URL+"/HomeView/viewvideo/id=927");

    }

    private class InsideWebViewClient extends WebViewClient
    {
        @Override
        // Force links to be opened inside WebView and not in Default Browser
        // Thanks http://stackoverflow.com/a/33681975/1815624
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);
            return true;
        }
    }

    @Override
    public void onBackPressed()
    {
        // Notify the VideoEnabledWebChromeClient, and handle it ourselves if it
        // doesn't handle it
        if (!webChromeClient.onBackPressed())
        {
            if (webView.canGoBack())
            {
                webView.goBack();
            }
            else
            {
                // Standard back button implementation (for example this could
                // close the app)
                super.onBackPressed();
            }
        }
    }
    @Override
    public void onPause() {
    	// TODO Auto-generated method stub
    	super.onPause();
    	webView.reload();
    }

}
